package dao.Impl;

import dao.Base;
import dao.ProductDao;
import entity.Product;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl extends Base implements ProductDao {
    @Override
    public List<Product> listProduct() {
        List<Product> list= new ArrayList<>();
        try{
            conn = super.getConn();
            String sql = "SELECT id,name,`desc`,price,flag from product";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                Product product = new Product();
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setDesc(rs.getString("desc"));
                product.setPrice(rs.getDouble("price"));
                product.setFlag(rs.getInt("flag"));
                list.add(product);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return list;
    }

    @Override
    public int addProduct(Product product) {
        int count = 0;
        try{
            conn = super.getConn();
            String sql = "INSERT INTO `microblng`.`product`(`name`, `desc`,`price`, `flag`) VALUES (?,?,?,?);";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,product.getName());
            pstmt.setString(2,product.getDesc());
            pstmt.setDouble(3,product.getPrice());
            pstmt.setInt(4,product.getFlag());

            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return count;
    }

    @Override
    public Product pid(int id) {
        Product product = null;
        try{
            conn = super.getConn();
            String sql = "SELECT id,name,`desc`,price,flag from product where id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            rs = pstmt.executeQuery();
            while (rs.next()){
                product = new Product();
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setDesc(rs.getString("desc"));
                product.setPrice(rs.getDouble("price"));
                product.setFlag(rs.getInt("flag"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return product;
    }

    @Override
    public int delete(int id) {
        int count = 0;
        try{
            conn = super.getConn();
            String sql = "DELETE FROM product WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return count;
    }
//    UPDATE `microblng`.`product` set `flag` = 1 WHERE `id` = 4;
}
